#pragma once
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <ctime>

namespace mylog
{
    class DateUtil
    {
    public:
        static time_t Now()
        {
            return time(nullptr);
        }
    };
    class FileUtil
    {
    public:
        static bool Exists(const std::string &pathname)
        {
            struct stat st;
            if (stat(pathname.c_str(), &st) < 0)
            {
                return false;
            }
            return true;
        }
        static std::string Getpath(const std::string &pathname)
        {
            size_t pos = pathname.find_last_of("/\\");
            if (pos == std::string::npos)
            {
                return ".";
            }
            return pathname.substr(0, pos + 1);
        }
        static void CreateDirectory(const std::string &pathname)
        {
            int index = 0;
            while (index < pathname.size())
            {
                size_t pos = pathname.find_first_of("/\\", index);
                if (pos == std::string::npos)
                {
                    mkdir(pathname.c_str(), 0777);
                    break;
                }
                std::string path = pathname.substr(0, pos + 1);
                if (Exists(path) == true)
                {
                    index = pos + 1;
                    continue;
                }
                mkdir(path.c_str(), 0777);
                index = pos + 1;
            }
        }
    };
}